package james.edu.gui;
import james.edu.to.FlashCardTO;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

public class Utils {
	
	public static List loadImages(String filename) {
		
		FileReader fr = null;
		ArrayList list = new ArrayList();
		int NUMBER_OF_COLUMNS_IN_FILE = 2;
		
		try {
			fr = new FileReader(filename);
		}
		catch (FileNotFoundException fnf) {
			System.out.println("file: " + filename + " not found ");
			return null;
		}
		
		BufferedReader br = new BufferedReader(fr);
		
		String line = null;
		try {
			while ((line = br.readLine()) != null) {
				String[] tokens = line.split(",");
			    if(tokens.length != NUMBER_OF_COLUMNS_IN_FILE) {	    
			    	System.out.println("bad number of columns in file");
			    	continue;
			     }			
			    else {
			    	String imageFile = tokens[1].trim();
			    	URL url = null;
			    	ImageIcon image = null;
			    	
			    	String desc = tokens[0];
			    	System.out.println("adding description: " + desc);
			    	
			    	list.add(new FlashCardTO(imageFile,desc));
			    			
			    	
			    	
			    }
			}
		} catch (IOException ioe) {
            ioe.printStackTrace();
		}
		
		try {
			fr.close();
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
		return list;
	}
	
	public static void main(String args []) {
		Utils.loadImages("c:/temp/flashCards.txt");
	}

}
